from rich.progress import Progress, BarColumn, SpinnerColumn, TimeRemainingColumn, TimeElapsedColumn
from rich.console import Console
import shutil  # 用于获取终端宽度

"""创建共享progress实例"""
progress_x = None

"""Rich progress 初始化"""
def progress_init():
    # # 获取当前终端宽度（如果无法获取，默认 80）
    # terminal_width = shutil.get_terminal_size().columns or 80
    # # 设置进度条总宽度为终端宽度的 80%（但不小于 60，不大于 120）
    # progress_width = max(60, min(int(terminal_width * 0.8), 200))
    console = Console(width=360)
    return Progress(
        "[progress.description]{task.description}({task.completed}/{task.total})",
        SpinnerColumn(finished_text="[green]✔"),
        BarColumn(),
        "[progress.percentage]{task.percentage:>3.2f}%",
        "[yellow]⏱",
        TimeElapsedColumn(),
        "[cyan]⏳",
        TimeRemainingColumn(),
        console = console
    )

def progress_update(future, #future回调函数必传参数，由函数自动传入
                progress, task_id, advance):
    progress.update(task_id, advance=advance)  # 每次完成1个任务，进度+1